<template>
  <div class="inquiry-detail-container">
    <h1 style="font-size: 24px;">{{ isEditing ? '编辑询单' : '新增询单' }}</h1>
    <el-form :model="inquiryForm" :rules="rules" ref="inquiryForm" label-width="140px" label-position="left" class="custom-form">
      <el-form-item label="询单编号" prop="inquiryId">
        <el-input v-model="inquiryForm.inquiryId" disabled></el-input>
      </el-form-item>
      <el-form-item label="商品信息" prop="productInfo">
        <el-input v-model="inquiryForm.productInfo" :disabled="!isEditing"></el-input>
      </el-form-item>
      <el-form-item label="预估采购数量" prop="estimatedQuantity">
        <el-input v-model="inquiryForm.estimatedQuantity" :disabled="!isEditing"></el-input>
      </el-form-item>
      <el-form-item label="需求描述" prop="description">
        <el-input type="textarea" v-model="inquiryForm.description" :disabled="!isEditing"></el-input>
      </el-form-item>
      <el-form-item label="询单渠道" prop="channel">
        <el-input v-model="inquiryForm.channel" :disabled="!isEditing"></el-input>
      </el-form-item>
      <el-form-item v-if="isEditing">
        <el-button type="primary" :loading="loading" @click="handleSubmit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      inquiryForm: {
        inquiryId: this.$route.query.id || '',
        productInfo: '',
        estimatedQuantity: '',
        description: '',
        channel: ''
      },
      rules: {
        productInfo: [
          { required: true, message: '请输入商品信息', trigger: 'blur' }
        ],
        estimatedQuantity: [
          { required: true, message: '请输入预估采购数量', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入需求描述', trigger: 'blur' }
        ],
        channel: [
          { required: true, message: '请输入询单渠道', trigger: 'blur' }
        ]
      },
      isEditing:true,
      loading: false
    };
  },
  created()  {
    // 假设从路由参数获取询单ID
    const inquiryId = this.$route.query.id;;
    this.isEditing = this.$route.query.type==='detail'?false:true;
    
    if (inquiryId) {
      // 这里可以添加根据询单ID获取询单详情的逻辑
      this.inquiryForm = {
        inquiryId: inquiryId,
        productInfo: '笔记本电脑',
        channel: '在线平台',
        estimatedQuantity: 10,
        description: '高性能笔记本电脑，用于办公',
        files: [],
        contact: '张三',
        phone: '13800138000',
        remark: '请尽快发货'
      };
    }
  },
  methods: {
    handleSubmit() {
      this.$refs.inquiryForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          // 这里可以添加提交询单的逻辑
          setTimeout(() => {
            this.$message({
              message: '询单已提交',
              type: 'success'
            });
            this.$router.push({ path: '/home/InquiryManagement' });
            this.loading = false;
          }, 1000);
        } else {
          this.$message({
            message: '请检查表单填写',
            type: 'error'
          });
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.inquiry-detail-container {
  padding: 20px;
}

.custom-form {
  margin: 20px 0;
  padding: 20px;
  background-color: #f9f9f9;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.el-form-item {
  margin-bottom: 20px;
}

.el-input {
  width: 100%;
}

.el-textarea {
  width: 100%;
}
</style>